package com.bosc.modeling.config;

import com.bosc.modeling.interceptor.ConfigInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${file.staticAccessPath}")
    private String staticAccessPath;


    @Value("${file.uploadFolder}")
    private String uploadFolder;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("*").maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder);
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
    /**
     * 注入第一步定义好的拦截器
     */
    @Autowired
    private ConfigInterceptor configInterceptor;

    /**
     * 定义拦截规则, 根据自己需要进行配置拦截和排除的接口
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(configInterceptor)
                // .addPathPatterns() 是配置需要拦截的路径
                .addPathPatterns("/**")
                // .excludePathPatterns() 用于排除拦截
                .excludePathPatterns("/index.html") // 排除127.0.0.1进入登录页
                .excludePathPatterns("/html/login.html") // 排除登录页
                .excludePathPatterns("/html/register.html") // 排除注册页
                .excludePathPatterns("/html/bg/login.html")
                .excludePathPatterns("/html/reset_password.html")
                .excludePathPatterns("/html/competition.html")
                .excludePathPatterns("/html/enroll.html")
                .excludePathPatterns("/html/competition_format.html")
                .excludePathPatterns("/html/competition_data.html")
                .excludePathPatterns("/html/sort.html")
                .excludePathPatterns("/sort_list")
                .excludePathPatterns("/reset_password")
                .excludePathPatterns("/login")
                .excludePathPatterns("/register")
                .excludePathPatterns("/check_login_state")
                .excludePathPatterns("/competition_list")
                .excludePathPatterns("/get_competition_by_id")
                .excludePathPatterns("/lib/**") // 排除静态文件
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/fonts/**")
                .excludePathPatterns("/css/**");
    }

}
